package com.ece.core.editors.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class EnterCommentHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
	Display display = new Display();
	final Shell shell = new Shell(display, SWT.APPLICATION_MODAL
		| SWT.DIALOG_TRIM);
	shell.setSize(800, 600);
	GridLayout gl = new GridLayout();
	gl.numColumns = 2;
	shell.setLayout(gl);

	/* review ID */
	Label reviewIDLabel = new Label(shell, SWT.BORDER);
	reviewIDLabel.setText("Review ID");
	reviewIDLabel
		.setToolTipText("This is the review id - or change request id");
	Text reviewID = new Text(shell, SWT.NONE);
	reviewID.setBounds(100, 50, 100, 20);
	reviewID.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
	reviewIDLabel.pack();
	reviewID.pack();
	GridData reviewLabelData = new GridData();
	reviewLabelData.widthHint = 100;
	reviewIDLabel.setLayoutData(reviewLabelData);
	GridData reviewData = new GridData(GridData.FILL_HORIZONTAL);
	reviewData.widthHint = 200;
	reviewID.setLayoutData(reviewData);

	/* review's name */
	Label reviewerLabel = new Label(shell, SWT.BORDER);
	reviewerLabel.setText("Reviewer");
	reviewerLabel.setToolTipText("This is the reviewer id / name");
	GridData reviewerLabelGridData = new GridData();
	reviewerLabelGridData.widthHint = 100;
	reviewerLabel.setLayoutData(reviewerLabelGridData);

	Text reviewerID = new Text(shell, SWT.NONE);
	reviewerID.setText("ssivasam"); // pull this from SVN
	reviewerID.setEditable(false);
	reviewerID.setBounds(100, 50, 100, 20);
	reviewerID.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
	GridData reviewerIDGridData = new GridData();
	reviewerIDGridData.widthHint = 200;
	reviewerID.setLayoutData(reviewerIDGridData);

	reviewerLabel.pack();
	reviewerID.pack();

	/* review */
	Label reviewLabel = new Label(shell, SWT.BORDER);
	reviewerLabel.setText("Review");
	reviewLabel.setToolTipText("This is the reviewer id / name");
	GridData reviewLabelGridData = new GridData();
	reviewLabelGridData.widthHint = 100;
	reviewLabel.setLayoutData(reviewLabelGridData);

	Text review = new Text(shell, SWT.NONE);
	review.setBounds(100, 50, 100, 20);
	review.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
	GridData reviewGridData = new GridData();
	reviewGridData.widthHint = 200;
	review.setLayoutData(reviewGridData);

	reviewLabel.pack();
	review.pack();

	/* ok / cancel button */
	Button okButton = new Button(shell, SWT.PUSH);
	okButton.addListener(SWT.Selection, new Listener() {

	    @Override
	    public void handleEvent(Event event) {
		// save data to the file system and
		// make calls to SVN module
	    }
	});
	okButton.setText("Add");

	Button cancelButton = new Button(shell, SWT.PUSH);
	cancelButton.addListener(SWT.Selection, new Listener() {

	    @Override
	    public void handleEvent(Event event) {
		shell.dispose();
	    }
	});
	cancelButton.setText("Cancel");

	okButton.pack();
	cancelButton.pack();

	shell.pack();
	shell.open();

	while (!shell.isDisposed()) {
	    if (!display.readAndDispatch())
		display.sleep();
	}

	// MessageDialog.openConfirm(shell, "Enter Review Dialog",
	// "Enter reivew");

	return null;
    }

    @Override
    public boolean isEnabled() {
	return true;

    }

}
